package com.magnificent.atlantis.client.data.message;

import com.magnificent.atlantis.client.data.configuration.AbstractConfiguration;
import com.magnificent.atlantis.client.data.configuration.ConfigurationContainer;
import com.magnificent.atlantis.client.data.configuration.ServerConfiguration;
import com.magnificent.atlantis.client.data.message.messages.*;
import com.magnificent.atlantis.client.data.settings.User;
import com.magnificent.atlantis.client.data.task.planner.CommunityMeetingPlannerTask;
import com.magnificent.atlantis.client.ui.planner.PlannerData;
import com.magnificent.atlantis.common.entity.messages.*;
import com.magnificent.commons.utils.collections.CollectionsUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * User: Babanin
 * Date: 04.05.2009
 * Time: 15:32:26
 */
public class ClientMessageHelper {

    public static String receiversToString(ClientMessage clientTextMessage, PlannerData plannerData) {
        AbstractConfiguration abstractConfiguration = plannerData.getConfigurationContainer().getConfigurationById(clientTextMessage.getConfigurationId());

        if (abstractConfiguration.isServerConfiguration()) {
            StringBuilder builder = new StringBuilder();
            ServerConfiguration serverConfiguration = (ServerConfiguration) abstractConfiguration;

            for (User user : serverConfiguration.getUsers(clientTextMessage.getUsersToIds())) {
                if (user == null) {
                    continue;
                }

                builder.append(user.getSurname()).append(" ").append(user.getName().toCharArray()[0]).append("., ");
            }

            return builder.toString().substring(0, builder.length() - 2);
        }

        return null;
    }

    public static String senderToString(ClientMessage clientMessage, ConfigurationContainer configurationContainer) {
        AbstractConfiguration abstractConfiguration = configurationContainer.getConfigurationById(clientMessage.getConfigurationId());

        if (abstractConfiguration.isServerConfiguration()) {
            ServerConfiguration serverConfiguration = (ServerConfiguration) abstractConfiguration;
            User sender = serverConfiguration.getUser(clientMessage.getUserFromId());

            if (sender == null) {
                return null;
            }

            return sender.getSurname() + " " + sender.getName().toCharArray()[0] + ".";
        }

        return null;
    }

    public static ClientMessage createMessageBy(ServerMessage newMessage, ServerConfiguration configuration) {
        if (newMessage instanceof ServerTextMessage) {
            return new ClientTextMessage((ServerTextMessage) newMessage, configuration);
        }

        if (newMessage instanceof ServerReInviteMessage) {
            return new ClientReInviteMessage((ServerReInviteMessage) newMessage, configuration);
        }

        if (newMessage instanceof ServerInviteMessage) {
            return new ClientInviteMessage((ServerInviteMessage) newMessage, configuration);
        }

        if (newMessage instanceof ServerAcceptMeetingMessage) {
            return new ClientAcceptMeetingMessage((ServerAcceptMeetingMessage) newMessage, configuration);
        }

        if (newMessage instanceof ServerCancelMeetingMessage) {
            return new ClientCancelMeetingMessage((ServerCancelMeetingMessage) newMessage, configuration);
        }

        if (newMessage instanceof ServerUpdateMeetingMessage) {
            return new ClientUpdateMeetingMessage((ServerUpdateMeetingMessage) newMessage, configuration);
        }

        if (newMessage instanceof ServerChangeMeetingTimeMessage) {
            return new ClientChangeMeetingTimeMessage((ServerChangeMeetingTimeMessage) newMessage, configuration);
        }

        return null;
    }


    public static ClientNewMeetingMessage createNewMeeting(CommunityMeetingPlannerTask task) {
        ClientNewMeetingMessage clientNewMeetingMessage = new ClientNewMeetingMessage();

        clientNewMeetingMessage.setDate(task.getStartDate());
        clientNewMeetingMessage.setSentDate(new Date());
        clientNewMeetingMessage.setMessage(task.getDescription());
        clientNewMeetingMessage.setMeetingTitle(task.getName());
        clientNewMeetingMessage.setInvitedUsers((ArrayList<Integer>) task.getInvitedUsers());
        clientNewMeetingMessage.setMessageType(MessageType.OUTBOX);
        clientNewMeetingMessage.setUsersToIds(task.getInvitedUsers());
        clientNewMeetingMessage.setLocalTaskId(task.getId());

        return clientNewMeetingMessage;
    }

    public static ClientMeetingMessage createDecisionMeetingMessage(ClientInviteMessage inviteMessage, String comment, boolean agree) {
        ClientMeetingMessage meetingMessage = agree ? new ClientAcceptMeetingMessage() : new ClientCancelMeetingMessage();

        meetingMessage.setComment(comment);
        meetingMessage.setSentDate(new Date());
        meetingMessage.setDate(inviteMessage.getDate());
        meetingMessage.setMessageType(MessageType.OUTBOX);
        meetingMessage.setMeetingId(inviteMessage.getMeetingId());
        meetingMessage.setMeetingTitle(inviteMessage.getMeetingTitle());
        meetingMessage.setConfigurationId(inviteMessage.getConfigurationId());
        meetingMessage.setUsersToIds(CollectionsUtils.list(inviteMessage.getUserFromId()));

        return meetingMessage;
    }

    public static ClientUpdateMeetingMessage createUpdateMeetingMessage(CommunityMeetingPlannerTask task, String oldTitle, boolean reInvite) {
        ClientUpdateMeetingMessage updateMeetingMessage = new ClientUpdateMeetingMessage();

        updateMeetingMessage.setOldTitle(oldTitle);
        updateMeetingMessage.setDate(task.getStartDate());
        updateMeetingMessage.setMeetingTitle(task.getName());
        updateMeetingMessage.setComment(task.getDescription());
        updateMeetingMessage.setMeetingId(task.getMeetingId());
        updateMeetingMessage.setMessageType(MessageType.OUTBOX);
        updateMeetingMessage.setInvitedUsers((ArrayList<Integer>) task.getInvitedUsers());
        updateMeetingMessage.setUsersToIds(task.getInvitedUsers());
        updateMeetingMessage.setSentDate(new Date());
        updateMeetingMessage.setLocalTaskId(task.getId());
        updateMeetingMessage.setReInvite(reInvite);

        return updateMeetingMessage;
    }

}
